package com.ny.controller;

import com.ny.entity.Classrooms;
import com.ny.entity.Users;
import com.ny.service.ClassroomService;
import com.ny.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ClassroomService classroomService;
//    如何接收用户和密码。使用 方法的参数进行接收
    @PostMapping ("/login")
    public String login(String username, String password, Model model){
        Users user =  userService.findUserByUsernameAndPassword(username,password);
        System.out.println(user);
         if(user!=null && user.getRole().equals("学生")){
//            查询教室的信息
            List<Classrooms>  classroomsList =  classroomService.findAllClassrooms();
             System.out.println(classroomsList);
//        如何把查询出来的数据交给jsp界面，交给model。model的名字：classroomsList
             model.addAttribute("classroomsList",classroomsList);
            return "classroom";
         }
         else{
             return "login";
         }
    }
}
